﻿using System.Collections.Generic;
using System.IO;

namespace PhiStore.Util
{
    /// <summary>
    /// Static class containing utility methods for handling streams
    /// </summary>
    public static class StreamUtil
    {
        /// <summary>
        /// Copies everything
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="bufferSize"></param>
        public static void Copy(Stream from, Stream to, int bufferSize)
        {
            byte[] buffer = new byte[bufferSize];

            int bytesRead = 0;
            while ((bytesRead = from.Read(buffer, 0, bufferSize)) > 0)
            {
                to.Write(buffer, 0, bytesRead);
            }
            to.Flush();
        }

        /// <summary>
        /// Copies everything with a buffer size set to 4096
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        public static void Copy(Stream from, Stream to)
        {
            Copy(from, to, 4096);
        }

        /// <summary>
        /// Read everything into a byte[]
        /// </summary>
        /// <param name="from"></param>
        /// <param name="bufferSize"></param>
        /// <returns></returns>
        public static byte[] Read(Stream from, int bufferSize)
        {
            List<byte> bytes = new List<byte>();

            byte[] buffer = new byte[bufferSize];
            int bytesRead = 0;
            while ((bytesRead = from.Read(buffer, 0, bufferSize)) > 0)
            {
                for (int i = 0; i < bytesRead; i++)
                {
                    bytes.Add(buffer[i]);
                }
            }
            return bytes.ToArray();
        }
        
        /// <summary>
        /// Read everything into a byte[] with a buffer size set to 4096
        /// </summary>
        /// <param name="from"></param>
        /// <returns></returns>
        public static byte[] Read(Stream from)
        {
            return Read(from, 4096);
        }

        /// <summary>
        /// Copies everything
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="bufferSize"></param>
        public static void Copy(byte[] from, Stream to, int bufferSize)
        {
            for(int i = 0; i < from.Length; i += bufferSize){
                if (i + bufferSize > from.Length)
                {
                    to.Write(from, i, from.Length - i);
                }
                else
                {
                    to.Write(from, i, bufferSize);
                }
            }
            to.Flush();
        }

        /// <summary>
        /// Copies everything with a buffer size set to 4096
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        public static void Copy(byte[] from, Stream to)
        {
            Copy(from, to, 4096);
        }


    }
}
